#include "listmaintence.h"


ListMaintence* ListMaintence::p_instance = 0;
ListMaintence::ListMaintence(QObject *parent) :
    QObject(parent)
{
    realmsMap.insert("ru",QList<QString>(std::initializer_list<QString> {"Eversong"}));
    loadCash();
    loadCollections();
    // collectionFillGo();

    connect(RequestManager::getInstance(),&RequestManager::itemData,
            [=](QMap<QString,QString>_s)
    {
        if (_s.values()[0] == "empty")
            collectionFillStep(0);
        else
        {
            qDebug()<<_s;
            itemInfoCash.insert(_s.value("id"),_s);
            itemUpdate();
            saveCash();
            saveCollections();
            //collectionFillStep(1);
        }
    });
}
ListMaintence::~ListMaintence()
{

}

int ListMaintence::addCollection(const QString &_s)
{
    QList<QString> _m;
    collections.insert(_s,_m);
    return 1;
}

int ListMaintence::remCollection(const QString &_c)
{
    collections.remove(_c);
}

int ListMaintence::addItem( const QString &_i)
{     
    RequestManager::getInstance()-> getItemInfo(_i);
    currentCollList.append(_i);

}

int ListMaintence::remItem(const QString &_i)
{
    qDebug()<<_i;
    currentCollList.removeAt( _i.toInt());
    saveCollections();
}
QString ListMaintence::getCollectionName() const
{
    return collectionName;
}

void ListMaintence::setCollectionName(const QString &value)
{
    collectionName = value;
    currentCollList = collections.value(collectionName);
    qDebug()<<":"<<collectionName;
    qWarning()<<"list"<<currentCollList;
}


QList<QString>* ListMaintence::collectionListContent()
{
    qDebug()<<"getting collection items content => "<<currentCollList ;
    return &currentCollList;
}

QMap<QString, QMap<QString, QString> > ListMaintence::collectionAssistList()
{
    return itemInfoCash;
}

QString ListMaintence::itemInfo(const QString &_i,const QString &_p)
{

    QString _s = itemInfoCash.value(_i).value(_p);
    if (_s !="")
        return _s;
    return"wait";
}

QList<QString> ListMaintence::collectionsList()
{
    QList<QString> _t  = collections.keys();
    _t.append("+");
    return _t;
}


int ListMaintence::collectionFillGo()
{

    foreach(QList<QString> _lst_001, collections)
    {
        foreach(QString _str_001, _lst_001)
        {
            qDebug()<<_str_001;
            if ( itemInfoCash.value( _str_001 ).isEmpty() )
                fillList.append( _str_001 );
        }
    }
    collectionFillStep(0);
    return 1;
}

int ListMaintence::collectionFillStep(bool m)
{   
    if ( m )
        fillList.removeAt(0);
    if(fillList.length())
    {
        RequestManager::getInstance()->getItemInfo(fillList.at(0));
    }
    return 1;
}

int ListMaintence::saveCash()
{
    QFile file("_data/cash.csh");
    if(file.open(QIODevice::WriteOnly))
    {
        QDataStream stream(&file);
        stream.setVersion(QDataStream::Qt_5_1);
        stream << itemInfoCash;
        file.flush();
        if(stream.status() == QDataStream::Ok)
            qDebug() << "Cash is save";
        else
            qDebug() << "Ошибка записи";
    }
    else
    {
        qDebug()<<file.errorString();
    }
    //   qDebug()<<itemInfoCash;
    file.close();
    return 1;
}

int ListMaintence::loadCash()
{
    QFile file("_data/cash.csh");
    if(file.open(QIODevice::ReadOnly))
    {
        QDataStream stream(&file);
        stream.setVersion (QDataStream::Qt_5_1) ;
        stream >> itemInfoCash;
        if(stream.status() == QDataStream::Ok)
            qDebug() << "Cash is load";
        else
            qDebug() << "Ошибка чтения файла";
        file.close();
    }
    return 1;
}

QList<QString> ListMaintence::realmsList(const QString& _s)
{
    return realmsMap.value(_s);
}
int ListMaintence::saveCollections()
{
    collections.insert(collectionName,currentCollList);
    qDebug()<<collections;
    QFile file("_data/lists/collections.f");
    if(file.open(QIODevice::WriteOnly))
    {
        QDataStream stream(&file);
        stream. setVersion(QDataStream::Qt_5_1);
        stream << collections;
        if(stream.status() != QDataStream::Ok)
        {
            qDebug() << "Ошибка записи";
        }
    }
    file.close();
    return 1;
}

int ListMaintence::loadCollections()
{
    qDebug()<<"load collections";
    QFile file("_data/lists/collections.f");
    if(file.open(QIODevice::ReadOnly))
    {
        QDataStream stream(&file);
        stream.setVersion (QDataStream::Qt_5_1) ;
        stream.device()->seek(0);
        stream >> collections;
        if(stream.status() != QDataStream::Ok)
        {
            qDebug() << "stream status is "<<stream.status();

        }

        file.close();
    }
    qDebug()<<"load collections done =>"<<collections;

    return 1;
}
